/*
 * ReplicaManagerModule.java    0.0.1    09/dic/2009
 * 
 * Copyright (c) 2009 mentalsmash.org
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * The use of the Apache License does not indicate that this project is
 * affiliated with the Apache Software Foundation.
 */ 
package org.mentalsmash.tazio.transparency.configuration;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;

import org.mentalsmash.tazio.commons.identifiers.NID;
import org.mentalsmash.tazio.transparency.impl.BaseInputDispatcher;
import org.mentalsmash.tazio.transparency.impl.BaseMessageFactory;
import org.mentalsmash.tazio.transparency.impl.BaseMessageFilter;
import org.mentalsmash.tazio.transparency.impl.BasePoint2PointCommunication;
import org.mentalsmash.tazio.transparency.impl.BaseTazioNetwork;
import org.mentalsmash.tazio.transparency.impl.TransparencyMonitorImpl;
import org.mentalsmash.tazio.transparency.impl.JGroupsGroupConnection;
import org.mentalsmash.tazio.transparency.impl.ReplicaManagerResolver;
import org.mentalsmash.tazio.transparency.interfaces.Dispatcher;
import org.mentalsmash.tazio.transparency.interfaces.GroupConnection;
import org.mentalsmash.tazio.transparency.interfaces.MessageFactory;
import org.mentalsmash.tazio.transparency.interfaces.MessageFilter;
import org.mentalsmash.tazio.transparency.interfaces.Point2PointCommunication;
import org.mentalsmash.tazio.transparency.interfaces.Resolver;
import org.mentalsmash.tazio.transparency.interfaces.TazioNetwork;
import org.mentalsmash.tazio.transparency.interfaces.TransparencyMonitor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.inject.AbstractModule;
import com.google.inject.name.Names;

/**
 * This extension to {@link AbstractModule} is used to create the bindings for the 
 * ReplicaManager's side of Tazio's transparency layer subsystem.
 * 
 * @version 0.0.1	09/dic/2009
 * @author Enrico Savioli <savioli.enrico@gmail.com>
 */
public class ReplicaManagerModule extends AbstractModule {

	private final static Logger log = LoggerFactory.getLogger(ReplicaManagerModule.class);
	
	private final String propertyFile;
	
	public ReplicaManagerModule(String propertyFile){
	    this.propertyFile = propertyFile;
	}
	
	
	/* (non-Javadoc)
	 * @see com.google.inject.AbstractModule#configure()
	 */
	@Override
	protected void configure() {

		try {
		    Properties props = loadProperties();
		    Names.bindProperties(binder(), props);
		} catch (IOException e) {
		    String msg = "Input/Output error while loading the configuration file";
		    log.error(msg);
		    e.printStackTrace();
		}
		
		bind(NID.class).annotatedWith(Names.named("node_id")).toInstance(NID.generateNID());
		
		bind(Dispatcher.class).to(BaseInputDispatcher.class);
		bind(MessageFactory.class).to(BaseMessageFactory.class);
		bind(MessageFilter.class).to(BaseMessageFilter.class).asEagerSingleton();
		bind(Resolver.class).to(ReplicaManagerResolver.class).asEagerSingleton();
		bind(Point2PointCommunication.class).to(BasePoint2PointCommunication.class);
		bind(TazioNetwork.class).to(BaseTazioNetwork.class);
		
		//group
		bind(GroupConnection.class).to(JGroupsGroupConnection.class);
		
		//test
		bind(TransparencyMonitor.class).to(TransparencyMonitorImpl.class);
	}
	
	private Properties loadProperties() throws IOException{
		
		Properties props = new Properties();

		FileInputStream inStream = new FileInputStream(propertyFile);
		props.load(inStream);
		inStream.close();

		return props;
		
	}


}
